package com.hngy.xpq.android_practice.ui.notifications;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.adapter.WalletAdapter;
import com.hngy.xpq.android_practice.bean.Wallet;
import com.hngy.xpq.android_practice.database.SQLiteWallet;
import com.hngy.xpq.android_practice.utils.WalletOrder;

import java.util.List;

public class WalletQuery extends AppCompatActivity {

    ListView list;
    ImageButton bin;
    SQLiteWallet sqLiteWallet;
    List<Wallet> lists,listing;
    SharedPreferences main,merchant;
    WalletAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wallet_query);

        bin = findViewById(R.id.wallet_query_bin);
        list = findViewById(R.id.wallet_query_list);

        main = super.getSharedPreferences("main",MODE_PRIVATE);
        merchant = super.getSharedPreferences("merchants",MODE_PRIVATE);
        QueryData();

        bin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WalletQuery.this,WalletActivity.class);
                startActivity(intent);
            }
        });
    }

    private void QueryData() {
        sqLiteWallet = new SQLiteWallet(this);
        showQueryData();
    }

    private void showQueryData() {
        if (lists != null){
            lists.clear();
        } else if (listing != null){
            listing.clear();
        }
        lists = sqLiteWallet.QueryWallet(main.getString("name",""));
        listing = sqLiteWallet.QueryWallet(merchant.getString("phone",""));
        if (main.getString("name","").equals("")){
            adapter = new WalletAdapter(this,listing);
        } else {
            adapter = new WalletAdapter(this,lists);
        }
        list.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==2){
            showQueryData();
        }
    }
}